package com.zkh.myutils.database.helper;

import java.util.ArrayList;
import java.util.List;

import com.zkh.myutils.database.enums.JoinType;

/**
 * 表对象，用来存储表名和联表信息
 * @author zkh
 */
public class Table {
	//表名
	private String tablename;
	/**
	 * 连接方式
	 */
	private JoinType joinType;
	/**
	 * 连接条件
	 */
	private List<ConditionPart> on;
	
	/**
	 * 构造方法，传入表名
	 * @param tablename 表名
	 */
	public Table(String tablename) {
		this.tablename = tablename;
	}
	
	/**
	 * 构造方法，传入表名
	 * @param tablename 表名
	 * @param joinType 联表类型
	 * @param on 联表条件
	 */
	public Table(String tablename, JoinType joinType) {
		this.tablename = tablename;
		this.joinType = joinType;
		this.on = new ArrayList<>();
	}
	
	/**
	 * 添加ON条件
	 * @return
	 */
	public Table on(ConditionPart... conditionParts) {
		for(ConditionPart p: conditionParts) {
			this.on.add(p);
		}
		return this;
	}
	
	/**
	 * 获取表
	 * @param paramList 参数容器，如果ON条件中有参数，将直接写入参数对象
	 * @return
	 */
	public String getTableString(List<Object> paramList) {
		//结果字符
		StringBuilder sb = new StringBuilder();
		//连接方式为空
		if(joinType==null) {
			sb.append(tablename);
		}else {
			//联表
			sb.append(joinType.desc).append(' ').append(tablename).append(" ON ");
			//条件
			ConditionPartResult part = ConditionPartResult.getConditionPartString(on);
			//追加
			sb.append(part.getCondition());
			paramList.addAll(part.getParamList());
		}
		//返回
		return sb.toString();
	}
	
	public String getTablename() {
		return tablename;
	}
	public void setTablename(String tablename) {
		this.tablename = tablename;
	}
	public JoinType getJoinType() {
		return joinType;
	}
	public void setJoinType(JoinType joinType) {
		this.joinType = joinType;
	}
	public List<ConditionPart> getOn() {
		return on;
	}
	public void setOn(List<ConditionPart> on) {
		this.on = on;
	}
}
